package org.zjt.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.ASMSerializerFactory;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.util.ASMClassLoader;
import lombok.Data;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * @author juntao.zhang
 * @version V1.0
 * @Description: TODO
 * @Package org.zjt.json
 * @date 2018/11/10 20:10
 */
@Data
public class BeanOne {

    private BeanTwo beanTwo;


    public static void main(String[] ARGS) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        BeanOne beanOne =new BeanOne();
        BeanTwo beanTwo = new BeanTwo();
        beanOne.setBeanTwo(beanTwo);
        beanTwo.setBeanOne(beanOne);

        //json 相互都依赖的循环依赖。但不会出现异常，也不会json循环。变成ref
        System.out.println(JSON.toJSONString(beanOne));



        //asmFactory
        SerializeConfig globalInstance = SerializeConfig.getGlobalInstance();
        Field asmFactory = globalInstance.getClass().getDeclaredField("asmFactory");
        asmFactory.setAccessible(true);
        ASMSerializerFactory asmSerializerFactory = (ASMSerializerFactory) asmFactory.get(globalInstance);
        Field classLoader = asmSerializerFactory.getClass().getDeclaredField("classLoader");
        classLoader.setAccessible(true);
        ASMClassLoader asmClassLoader = (ASMClassLoader) classLoader.get(asmSerializerFactory);


        Class<?> aClass = Class.forName("com.alibaba.fastjson.serializer.ASMSerializer_1_BeanOne",true,asmClassLoader);


        Arrays.stream(aClass.getDeclaredMethods()).forEach(System.err::println);
    }
}
